
name: nasos

on:
  push:
    branches: [ "main" ]

  workflow_dispatch:

jobs:

  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
        - name: Checkout
          uses: actions/checkout@v2     
        - name: Login to github registry
          uses: docker/login-action@v2
          with:
            registry: ghcr.io
            username: ${{ github.actor }}
            password: ${{ secrets.GITHUB_TOKEN }}
        - name: get build number
          uses: benjlevesque/short-sha@v1.2
          id: short-sha
          with:
           length: 7
        - name: Get git commit data
          uses: rlespinasse/git-commit-data-action@v1           
        - name : build docker image and push
          uses: docker/build-push-action@v3
          with:
            context: .
            file: ./Dockerfile
            push: true
            tags: |
              ghcr.io/${{ github.repository }}/${{ github.workflow }}:${{ steps.short-sha.outputs.sha }}
        - name: change latest image tags
          uses: dacbd/gha-secrets@v1
          with:
            token: ${{ secrets.GITHUB_TOKEN }}
            name: LAST_IMAGE_TAG
            value: ${{ steps.short-sha.outputs.sha }}
        - name: Send notification
          uses: dawidd6/action-send-mail@v2
          with:
            server_address: smtp.gmail.com
            server_port: 465
            username: ${{ secrets.EMAIL_SENDER_USERNAME }}
            password: ${{ secrets.EMAIL_SENDER_PASSWORD }}
            subject: ${{ github.job }} job of ${{ github.repository }} has ${{ job.status }}
            body: ${{ github.job }} job in workflow ${{ github.workflow }} of ${{ github.repository }} of backend has ${{ job.status }} has tag of secrets ${{ secrets.LAST_IMAGE_TAG }}
            to: ${{ env.GIT_COMMIT_COMMITTER_EMAIL }}
            from: ${{ env.GIT_COMMIT_AUTHOR }}
  
  provise:
    runs-on: ubuntu-latest
    env:
      MONGODB_DB: ${{ secrets.MONGODB_DB }}
      MONGODB_LOGIN: ${{ secrets.MONGODB_LOGIN }}
      MONGODB_PASSWORD: ${{ secrets.MONGODB_PASSWORD }}
    needs: [build]
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Run playbook
      uses: dawidd6/action-ansible-playbook@v2
      with:
        playbook: ansible-provise.yaml
        directory: ./
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        inventory: ${{ secrets.INVENTORY }}
  
  deploy:
    runs-on: ubuntu-latest
    needs: [provise]
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: add docker-compose
      uses: cuchi/jinja2-action@v1.2.0
      with:
        template: docker-compose.j2
        output_file: docker-compose.yaml
      env:
        MONGOUSER: ${{ secrets.MONGODB_USER }}
        MONGOPASS: ${{ secrets.MONGODB_PASS }}
        MONGOTAG: ${{ secrets.LAST_IMAGE_TAG }}   
    - name: ssh scp ssh pipelines
      uses: cross-the-world/ssh-scp-ssh-pipelines@latest
      env:
        WELCOME: "ssh scp ssh pipelines"
        LASTSSH: "Doing something after copying"
      with:
        host: ${{ secrets.SSH_HOST }}
        user: ${{ secrets.SSH_HOST_USER }}
        key: ${{ secrets.SSH_PRIVATE_KEY }}
        connect_timeout: 10s
        first_ssh: |
          CHECK=$(ls | wc -l) ; if [ $CHECK -eq 1 ]; then docker-compose down && rm *; else echo "initial setup"; fi
        scp: |
          ./docker-compose.yaml => ./
        last_ssh: |
          docker-compose up -d